package com.hzyb.oldpack.controller;

import com.hzyb.oldpack.service.*;
import com.hzyb.util.CharUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HzybController {

    private final static Logger Logger = LoggerFactory.getLogger(HzybController.class);

    @Resource
    private ShopFileService ShopFileService;

    @Resource
    private VshopCarouselService VshopCarouselService;

    @Resource
    private ShopCategorieService ShopCategorieService;

    @Resource
    private ShopNewProductService ShopNewProductService;

    @Resource
    private ShopCartService ShopCartService;


    @Resource
    private ShopRecommendListService shopRecommendListService;

    @Resource
    private ShopOrderService ShopOrderService;


    @RequestMapping("/upfilejsp")
    public ModelAndView upFilejsp(@RequestParam String type, @RequestParam String lickey) {
        ModelAndView modelAndView = new ModelAndView("upfile");
        modelAndView.addObject("type", type);
        modelAndView.addObject("lickey", lickey);
        return modelAndView;
    }

    /**
     * @param upFile
     * @param type
     * @param licKey
     * @return
     * @throws Exception 文件上传接口，type为项目文件根目录下的子目录名称可以以xxx/xxx/xxx格式命名，lickey暂时预留，后期出于安全性考虑校验用
     */
    @RequestMapping("/upFile")
    public Map<String, Object> upFile(@RequestParam("file") MultipartFile upFile, @RequestParam String type, @RequestParam String licKey) {
        Logger.info("开始上传文件---->" + upFile.getOriginalFilename());
        if (CharUtil.isChinese(type)) {
            Map<String, Object> map = new HashMap<>();
            map.put("status", "fail");
            map.put("error_msg", "路径中包含中文");
            return map;
        }
        String filename = upFile.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."), filename.length());
        filename = filename.substring(0, filename.lastIndexOf("."));
        try {
            return ShopFileService.uploadFile(upFile.getBytes(), filename, suffix, type);
        } catch (Exception e) {
            Logger.error(e.getMessage());
            return null;
        }
    }

    /**
     * 轮播服务
     *
     * @return 轮播图片url
     */
    @RequestMapping("/carouselService")
    public Map<String, Object> carouselService() {
        Map<String, Object> map = new HashMap<>();
        map.put("data", VshopCarouselService.getshopCarousel());
        return map;
    }


    /**
     * 获取商品分类
     *
     * @return 商品分类
     */
    @RequestMapping("/getShopCategories")
    public Map<String, Object> getShopCategories() {
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("data", ShopCategorieService.getShopCategories());
            map.put("status", "success");
        } catch (Exception e) {
            map.put("sttaus", "fail");
            map.put("message", "DataSourceFail!");
            Logger.error(e.toString());
        }
        return map;
    }

    /**
     * 取得上新商品
     *
     * @return
     */
    @RequestMapping("/getnewProduct")
    public Map<String, Object> getNewProduct(@RequestParam String is_more) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", "fail");
        try {
            map.putAll(ShopNewProductService.getNewProduct(is_more));
            map.put("status", "success");
        } catch (Exception e) {
            // TODO: handle exception
            Logger.error(e.toString());
        }
        return map;
    }


    /**
     * 取得物品信息
     *
     * @return
     */
    @RequestMapping("/getitemInformation")
    public Map<String, Object> getitemInformation(String categories_sid) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", "fail");
        try {
            map.put("data", ShopCategorieService.getItemInformation(categories_sid));
            map.put("status", "success");
        } catch (Exception e) {
            // TODO: handle exception
            Logger.error(e.toString());
        }
        return map;
    }


    /**
     * 取得指定用户的购物车
     *
     * @param openId
     * @return
     */
    @RequestMapping("/getShopCart")
    public Map<String, Object> getShopCart(@RequestParam String openId) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", "fail");
        try {
            map.put("data", ShopCartService.getShopCart(openId));
            map.put("status", "success");
        } catch (Exception e) {
            // TODO: handle exception
            Logger.error(e.toString());
        }
        return map;
    }


    /**
     * 取得推荐列表
     *
     * @return
     */
    @RequestMapping("/getShopRecommendList")
    public Map<String, Object> getShopRecommendList() {
        Map<String, Object> map = new HashMap<>();
        map.put("status", "fail");
        try {
            map.put("data", shopRecommendListService.getShopRecommendList());
            map.put("status", "success");
        } catch (Exception e) {
            // TODO: handle exception
            Logger.error(e.toString());
        }
        return map;
    }

    @RequestMapping("/getAllOrderList")
    public Map<String, Object> getAllOrderList(@RequestParam String openId) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", "fail");
        try {
            map.put("data", ShopOrderService.getAllOrderList(openId));
            map.put("status", "success");
        } catch (Exception e) {
            // TODO: handle exception
            Logger.error(e.toString());
        }
        return map;
    }

}

